import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class P2PRadioTest {

	/**
	 * @param args
	 *  args[0] = Number of peers
	 *  args[1] = Churn rate
	 */
	private RadioPeer host = null;
	private RadioPeer simplePeer;
	private String hostingAddress;

	public static void main(String[] args) {
		P2PRadioTest test = new P2PRadioTest();
		if(args.length>0) {
			int port = Integer.parseInt(args[1]);
			test.createSimplePeer(args[0],port);
		} else {
			test.createHostPeer();
		}
		test.evaluateMode();		
	}

	public void createSimplePeer(String host, int hostPort) {
		int port = new Random().nextInt(9000);
		int capacity = new Random().nextInt(4)+1;
		String name = "peer" + new Random().nextInt();
		hostingAddress = "tcp://" + host + ":" + hostPort;
		CapacityModule capacityModule = new CapacityModule(capacity);
		simplePeer = new RadioPeer(name, port, capacityModule);
		simplePeer.setRootAddress(hostingAddress);
		simplePeer.setMyAddress(getHostAddress());
		Thread t = new Thread(simplePeer);
		t.start();
	}

	public void createHostPeer(){
		int port = 1000;
		int capacity = new Random().nextInt(4)+1;
		String name = "peer" + new Random().nextInt();
		CapacityModule capacityModule = new CapacityModule(capacity);
		host = new RadioPeer(name,port,capacityModule);
		hostingAddress = getHostAddress();
		host.setMyAddress(hostingAddress);
		Thread t = new Thread(host);
		t.start();
	}

	public String getHostAddress() {
		String hostAddress = "";
		try {
			hostAddress = InetAddress.getLocalHost().getHostAddress();		
		} catch (UnknownHostException e) {}
		return hostAddress;
	}

	private List<RadioPeer.Peer> peerList;

	public void evaluateMode() {
		System.out.println("Type 'eval' for evaluation");
		System.out.println("Type 'disc' to disconnect a random peer");

		while (true) {
			Scanner input = new Scanner(System.in);
			String inputLine = input.nextLine();


			if(inputLine.equals("PeerChild")){
				for(RadioPeer.Peer peer: peerList){
					System.out.println(peer.getPort()+": " + peer.getChildren().get(0).getPeerID());
				}
			}
			else if (inputLine.equals("disc")) {
				int random = new Random().nextInt(5);
				if(simplePeer!=null) {
					simplePeer.doDaStopping();
				} else if(host!=null) {
					host.doDaStopping();
				}

				System.out.println("Disconnecting peer-" + random);
			}
			else if (inputLine.equals("graph")) {
				Graph graph = new Graph(host.getGraphPeers());
				graph.printGraph();
				graph.saveGraph();
				host.resetGraphPeers();
				System.out.println("graph initiated");
			}
			else if (inputLine.equals("evaluate")) {
				host.makeEvaluation();
				System.out.println("Evaluation started. Wait for a minute and call 'graph'");
			}
			else if (inputLine.equals("getHostAddress")) {
				System.out.println("Host address: " + hostingAddress + " 1000");
			}
			else if (inputLine.equals("getChildSize")) {
				System.out.println("Child 1: " + host.getChildren().size());
			}
			else if (inputLine.equals("getPort")) {
				System.out.println("Port: " + host.getPort());
			}
		}
	}
}
